<template>
  <c-view>
    <template v-slot:header>
      <div class="title">
        {{ $route.meta.title }}
        <el-button type="primary" :size="size" icon="el-icon-plus" @click="showDialog">新增车辆</el-button>
      </div>
    </template>
    <div class="main__box">
      <c-table
        ref="cTable"
        selection
        hasBorder
        :max-height="maxHeight"
        :size="size"
        :loading="isLoading"
        :table-header="tableHeader"
        :table-list="tableList"
        :table-inner-btns="tableInnerBtns"
        :page-info="pageInfo"
        @change-pagination="changePagination"
      >
        <template v-slot:header>
          <c-search :form-model="searchObj" :form-items="searchItems" @submit-form="searchSubmit" @reset-form="searchReset"></c-search>
        </template>
      </c-table>
    </div>
    <div v-if="dialogObj.isShow">
      <c-dialog
        class="dialog-box"
        :is-show="dialogObj.isShow"
        :title="dialogObj.title"
        close-btn
        @before-close="dialogObj.isShow = false"
        @on-submit="dialogConfirm"
      >
        <type-add ref="childRef" :init-data.sync="dialogObj.initData" :is-edit="dialogObj.isEdit"></type-add>
      </c-dialog>
    </div>
  </c-view>
</template>

<script>
import mixinTable from 'mixins/table'
import CDialog from 'components/dialog'
// import dictObj from '@/store/dictData'
import TypeAdd from './add'

export default {
  name: 'carManage',
  mixins: [mixinTable],
  components: {
    CDialog,
    TypeAdd
  },
  data(vm) {
    return {
      // 对话框对象
      dialogObj: {},
      // 表格内操作按钮
      tableInnerBtns: [
        {
          name: '编辑',
          icon: 'el-icon-edit',
          width: 130,
          handle(row) {
            const {
              id,
              carNumber,
              color,
              modelId,
              cid,
              cityCode,
              category,
              phone,
              owner,
              engineno,
              classno,
              speed,
              seats,
              carImguri,
              smallCarImgurl,
              mileage,
              annualVehicleInspectionTime
            } = row
            vm.showDialog({
              title: '编辑车辆类型',
              isShow: true,
              isEdit: true,
              initData: {
                id,
                carNumber,
                color,
                modelId,
                cid,
                cityCode,
                category,
                phone,
                owner,
                engineno,
                classno,
                speed,
                seats,
                carImguri,
                smallCarImgurl,
                mileage,
                annualVehicleInspectionTime
              }
            })
          }
        }
        // {
        //   name: '删除',
        //   icon: 'el-icon-delete',
        //   handle(row) {
        //     const { modelId, id } = row
        //     vm.confirmTip(`确定删除【${modelId}】车辆`, () => {
        //       vm.deleteHandle({ id })
        //     })
        //   }
        // }
      ],
      tableHeader: [
        {
          label: '车牌号码',
          prop: 'carNumber',
          width: 100,
          fixed: true,
          search: {
            type: 'input'
          }
        },
        {
          label: '所属城市',
          prop: 'cityName',
          width: 100
        },
        {
          label: '所属企业',
          prop: 'companyName'
        },
        {
          label: '设备编号',
          prop: 'cid'
        },
        {
          label: '车辆图片',
          prop: 'smallCarImgurl',
          width: 100,
          isImage: true
        },
        // {
        //   label: '车辆图片',
        //   prop: 'carImgUri',
        //   width: 100,
        //   isImage: true
        // },
        {
          label: '车辆品牌',
          prop: 'brandName',
          width: 100
        },
        {
          label: '车辆型号',
          prop: 'carTypeType',
          width: 100
        },
        {
          label: '车辆类型',
          prop: 'category',
          formatter: row => row && vm.setTableColumnLabel(row.category, 'carTypeList')
        },
        {
          label: '车身颜色',
          prop: 'color',
          width: 100
        },
        {
          label: '发动机号',
          prop: 'engineno',
          width: 120
        },
        {
          label: '车架号',
          prop: 'classno',
          width: 170
        },
        {
          label: 'SIM卡号',
          prop: 'phone',
          width: 110
        },
        {
          label: '座位数',
          prop: 'seats',
          width: 100
        },
        {
          label: '剩余续航(KM)',
          prop: 'enduranceMileage',
          width: 110
        },
        {
          label: '行驶总里程(KM)',
          prop: 'mileage',
          width: 130
        },
        {
          label: '最高时速(KM/H)',
          prop: 'speed',
          width: 130
        },
        {
          label: '车辆归属',
          prop: 'owner',
          width: 100,
          formatter: row => row && vm.setTableColumnLabel(row.owner, 'carOwnerList')
        },
        {
          label: '车辆年检时间',
          prop: 'annualVehicleInspectionTime',
          width: 160
        },
        {
          label: '创建时间',
          prop: 'createTime',
          width: 160
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    /*
     * 查询表格列表数据
     */
    fetchData() {
      const { pageNo, pageSize } = this.pageInfo
      const { dateTime, ...other } = this.searchObj
      const searchDate = this.getSearchDate(dateTime, 'dateTime')
      this.isLoading = true
      this.$api.vehicle
        .fetch('carMrgList', {
          ...searchDate,
          ...other,
          rows: pageNo,
          offset: pageSize
        })
        .then(res => {
          this.isLoading = false
          if (res && res.totalCount) {
            const { data, totalCount } = res
            this.pageInfo.totalNum = totalCount
            this.tableList = data || []
          } else {
            this.tableList = res || []
          }
        })
    },
    /**
     * dialog对话框数据处理
     * @opts {*}
     */
    showDialog(opts) {
      this.dialogObj = {
        isShow: true,
        isEdit: opts.isEdit || false,
        title: opts.title || '新增车辆类型',
        initData: opts.initData
      }
    },
    /**
     * dialog新增、编辑处理函数
     */
    dialogConfirm() {
      const childRef = this.$refs.childRef
      childRef.$refs.formRef.validate(valid => {
        if (valid) {
          const params = childRef.formModel
          // const { smallFileList, fileList, ...other } = childRef.formModel
          const curAction = this.dialogObj.isEdit ? 'carMrgEdit' : 'carMrgSave'
          this.$api.vehicle
            .fetch(curAction, {
              ...params,
              smallCarImgurl: 'https://yccl.oss-cn-shenzhen.aliyuncs.com/carImg/common.jpg'
              // carImguri: fileList[0].url,
              // ...other
            })
            .then(() => {
              this.responeHandle(this.dialogObj.isEdit ? '更新成功' : '新增成功')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    /**
     * 删除单条表格数据
     * @id {Number}
     */
    deleteHandle(params) {
      this.$api.vehicle.fetch('', params).then(() => {
        this.$msgTip('删除成功')
        this.delResetData()
      })
    }
  }
}
</script>
